//
//  ShoppingDetailViewController.m
//  JewelryStore
//
//  Created by apple on 2016/11/7.
//  Copyright © 2016年 com. All rights reserved.
//

#import "ShoppingDetailViewController.h"
#import "TitleImageButton.h"
#import "HomeScrollView.h"
#import "BaseLabel.h"

@interface ShoppingDetailViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, SDCycleScrollViewDelegate>
@property (nonatomic, strong) UICollectionView *shoppingDetailCollectionView;


@property (nonatomic, copy) NSArray *imageURL; //假数据
@end

@implementation ShoppingDetailViewController

#pragma mark - ViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setupNavigationBar];
    [self setupBottomBar];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.shoppingDetailCollectionView];
    self.automaticallyAdjustsScrollViewInsets = NO;

}

#pragma mark - 懒加载

- (UICollectionView *)shoppingDetailCollectionView
{
    if (!_shoppingDetailCollectionView) {
        //布局
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 1;
        layout.minimumInteritemSpacing = 1;
        
        _shoppingDetailCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-49-64) collectionViewLayout:layout];
        _shoppingDetailCollectionView.dataSource = self;
        _shoppingDetailCollectionView.delegate = self;
        _shoppingDetailCollectionView.backgroundColor = kBackgroundGray;
        _shoppingDetailCollectionView.showsVerticalScrollIndicator = NO;
        
        //注册cell
        [_shoppingDetailCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        
        //注册head
        [_shoppingDetailCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
        
    }
    return _shoppingDetailCollectionView;
}

- (NSArray *)imageURL
{
    if (!_imageURL) {
        _imageURL = @[@"http://pic.4j4j.cn/upload/pic/20130815/31e652fe2d.jpg", @"http://pic.4j4j.cn/upload/pic/20130909/681ebf9d64.jpg", @"http://img3.imgtn.bdimg.com/it/u=581864004,1701839937&fm=21&gp=0.jpg"];
    }
    return _imageURL;
}
#pragma mark - UICollectionView

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT/1.388);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(SCREEN_WIDTH/2.02, SCREEN_WIDTH/5);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *headView;
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head" forIndexPath:indexPath];
        [self initSectionUIWithReusableView:headView];
        headView.backgroundColor = [UIColor redColor];
    }
    return headView;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor blueColor];
    return cell;
    
}


#pragma mark - private

- (void)setupNavigationBar
{
    self.title = @"卡罗蒂戒指";
    self.view.backgroundColor = kWhite;
    UIBarButtonItem *shareItem = [[UIBarButtonItem alloc] initWithImage:KImageNamed(@"share") style:UIBarButtonItemStyleDone target:self action:@selector(shareClick)];
    self.navigationItem.rightBarButtonItem = shareItem;
}

- (void)setupBottomBar
{
    //背景
    UIView *backView = [[UIView alloc] init];
    [self.view addSubview:backView];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.and.bottom.equalTo(@0);
        make.height.equalTo(@49);
    }];
    
    //客服
    TitleImageButton *serviceBtn = [[TitleImageButton alloc] initWithTitle:@"客服" image:KImageNamed(@"service_btn") systemFont:Font(12)];
    [serviceBtn addTarget:self action:@selector(serviceClick) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:serviceBtn];
    [serviceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.and.bottom.equalTo(@0);
        make.width.equalTo(@(SCREEN_WIDTH/6.5));
    }];
    
    //收藏
    TitleImageButton *favoriteBtn = [[TitleImageButton alloc] initWithTitle:@"收藏" image:KImageNamed(@"favorite_btn") systemFont:Font(12)];
    [favoriteBtn addTarget:self action:@selector(favoriteClick) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:favoriteBtn];
    [favoriteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(serviceBtn.mas_right);
        make.top.and.bottom.equalTo(@0);
        make.width.equalTo(serviceBtn.mas_width);
    }];
    
    //购物车
    TitleImageButton *carBtn = [[TitleImageButton alloc] initWithTitle:@"购物车" image:KImageNamed(@"car_btn") systemFont:Font(12)];
    [carBtn addTarget:self action:@selector(carBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:carBtn];
    [carBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(favoriteBtn.mas_right);
        make.top.and.bottom.equalTo(@0);
        make.width.equalTo(favoriteBtn.mas_width);
    }];
    
    //加入购物车
    TitleImageButton *joinCar = [[TitleImageButton alloc] initWithTitle:@"加入购物车" backgroundColor:[UIColor orangeColor] systemFont:Font(17)];
    [joinCar addTarget:self action:@selector(joinCarClick) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:joinCar];
    [joinCar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(carBtn.mas_right);
        make.top.and.bottom.equalTo(@0);
        make.width.equalTo(@(SCREEN_WIDTH*3.5/13));
    }];
    
    //立即购买
    TitleImageButton *buyNow = [[TitleImageButton alloc] initWithTitle:@"立即购买" backgroundColor:[UIColor redColor] systemFont:Font(17)];
    [buyNow addTarget:self action:@selector(buyNowClick) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:buyNow];
    [buyNow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(joinCar.mas_right);
        make.top.and.bottom.equalTo(@0);
        make.width.equalTo(joinCar.mas_width);
    }];
}

- (void)initSectionUIWithReusableView:(UICollectionReusableView *)headView
{
    //轮播图
    CGRect scFrame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT/4);
    HomeScrollView *shoppingSC = [[HomeScrollView alloc] initWithFrame:scFrame imageURLStringsGroup:self.imageURL delegate:self isAutoScroll:NO];
    [headView addSubview:shoppingSC];
    
    //大背景
    UIView *backgroudView = [[UIView alloc] init];
    backgroudView.backgroundColor = [UIColor lightGrayColor];
    [headView addSubview:backgroudView];
    [backgroudView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(SCREEN_HEIGHT/4));
        make.left.and.right.and.bottom.equalTo(@0);
    
    }];
    
    //标题背景
    UIView *backTitleView = [[UIView alloc] init];
    [backgroudView addSubview:backTitleView];
    backTitleView.backgroundColor = [UIColor yellowColor];
    [backTitleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.and.right.equalTo(@0);
        make.height.equalTo(@(SCREEN_HEIGHT/5));
    }];
    
    //标题
    BaseLabel *titleLab = [[BaseLabel alloc] initWithTitle:@"卡罗蒂情侣戒指女925银活口对戒大家撒打发" color:[UIColor blackColor] font:Font(17)];
    [backTitleView addSubview:titleLab];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.and.top.equalTo(@10);
    }];
    
    //价格
    BaseLabel *priceLab = [[BaseLabel alloc] initWithTitle:@"￥189888" color:[UIColor redColor] font:Font(17)];
    [backTitleView addSubview:priceLab];
    [priceLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLab.mas_bottom).offset(10);
        make.left.equalTo(titleLab.mas_left);
    }];
    
    //市场价格
    BaseLabel *priceOldLab = [[BaseLabel alloc] initWithTitle:@"市场价:￥20999" color:[UIColor grayColor] font:Font(13)];
    priceOldLab.attributedText = [priceOldLab setupCenterLineWithOldStr:priceOldLab.text];
    [backTitleView addSubview:priceOldLab];
    [priceOldLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(priceLab.mas_centerY);
        make.left.equalTo(priceLab.mas_right).offset(10);
    }];
    
    //款号
    BaseLabel *styleLab = [[BaseLabel alloc] initWithTitle:@"款号:NE3003" color:[UIColor blackColor] font:Font(14)];
    [backTitleView addSubview:styleLab];
    [styleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(priceOldLab.mas_bottom).offset(10);
        make.left.equalTo(priceLab.mas_left);
    }];
    
    //快递
    BaseLabel *sendPriceLab = [[BaseLabel alloc] initWithTitle:@"快递:00" color:[UIColor lightGrayColor] font:Font(14)];
    [backTitleView addSubview:sendPriceLab];
    [sendPriceLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(styleLab.mas_left);
        make.top.equalTo(styleLab.mas_bottom).offset(10);
        make.width.equalTo(@((SCREEN_WIDTH-20)/3));
    }];
    
    //销量
    BaseLabel *numLab = [[BaseLabel alloc] initWithTitle:@"月销56" color:[UIColor lightGrayColor] font:Font(14)];
    numLab.textAlignment = UIBaselineAdjustmentAlignCenters;
    [backTitleView addSubview:numLab];
    [numLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(sendPriceLab.mas_right);
        make.top.equalTo(sendPriceLab.mas_top);
        make.width.equalTo(sendPriceLab.mas_width);
    }];
    
    //所在地
    BaseLabel *addressLab = [[BaseLabel alloc] initWithTitle:@"深圳" color:[UIColor lightGrayColor] font:Font(14)];
    addressLab.textAlignment = UIBaselineAdjustmentNone;
    [backTitleView addSubview:addressLab];
    [addressLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(numLab.mas_right);
        make.top.equalTo(numLab.mas_top);
        make.width.equalTo(numLab.mas_width);
    }];
    
    //地址背景
    UIView *backAddressView = [[UIView alloc] init];
    [backgroudView addSubview:backAddressView];
    backAddressView.backgroundColor = kWhite;
    [backAddressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(backTitleView.mas_bottom).offset(5);
        make.left.and.right.equalTo(@0);
        make.height.equalTo(@(SCREEN_HEIGHT/7));
    }];
    
    //送至
    BaseLabel *sendToLab = [[BaseLabel alloc] initWithTitle:@"送至" color:[UIColor lightGrayColor] font:Font(14)];
    [backAddressView addSubview:sendToLab];
    [sendToLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(styleLab.mas_left);
        make.top.equalTo(@20);
    }];
    
    //送至按钮
    TitleImageButton *sendToBtn = [[TitleImageButton alloc] initWithDefaultTitle:@" 广东省深圳市罗湖区田贝四路翠竹酒店5楼" image:KImageNamed(@"location-pin") systemFont:Font(14)];
    [sendToBtn addTarget:sendToBtn action:@selector(sendToClick) forControlEvents:UIControlEventTouchUpInside];
    [backAddressView addSubview:sendToBtn];
    [sendToBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(sendToLab.mas_right).offset(10);
        make.top.equalTo(sendToLab.mas_top);
    }];
    
    //售后
    TitleImageButton *afterBtn = [[TitleImageButton alloc] initWithDefaultTitle:@"店铺发货&售后" image:KImageNamed(@"xiaogou") systemFont:Font(14)];
    afterBtn.enabled = NO;
    [backAddressView addSubview:afterBtn];
    [afterBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(sendToLab.mas_left);
        make.top.equalTo(sendToLab.mas_bottom).offset(20);
    }];
    
    //货到
    
    //评价背景
    UIView *backCommentView = [[UIView alloc] init];
    [backgroudView addSubview:backCommentView];
    backCommentView.backgroundColor = [UIColor whiteColor];
    [backCommentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(backAddressView.mas_bottom).offset(5);
        make.left.and.right.equalTo(@0);
        make.height.equalTo(@(SCREEN_HEIGHT/9));
    }];

}

- (void)shareClick
{
    TT_Log(@"shareClick");
}

- (void)serviceClick
{
    TT_Log(@"serviceClick");
}

- (void)favoriteClick
{
    TT_Log(@"favoriteClick");
}

- (void)carBtnClick
{
    TT_Log(@"carBtnClick");
}

- (void)joinCarClick
{
    TT_Log(@"joinCarClick");
}

- (void)buyNowClick
{
    TT_Log(@"buyNowClick");
}

- (void)sendToClick
{
    TT_Log(@"sendToClick");
}
@end
